package org.todd.healthreport.service.Impl;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.todd.healthreport.service.IDongDaHealthReportService;
import org.todd.healthreport.util.EmailUtil;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class DongDaHealthReportServiceImpl extends BaseChromeDriver implements IDongDaHealthReportService {

    @Value("${dongda.loginPage}")
    private String loginPage;

    @Value("${dongda.indexPage}")
    private String indexPage;

    @Value("${dongda.userInfo}")
    private String userInfo;

    @Autowired
    private EmailUtil emailUtil;

    ChromeDriver chrome = null;

    @Override
    public void login(String userName, String password) {
        chrome = super.getChromeDriver();
        chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        chrome.get(loginPage);
        WebElement un = chrome.findElementById("un");
        un.sendKeys(userName);
        WebElement pd = chrome.findElementById("pd");
        pd.sendKeys(password);
        WebElement index_login_btn = chrome.findElementById("index_login_btn");
        index_login_btn.click();
    }

    @Override
    public void doReport() {
        chrome.get(indexPage);
        String[] radioList = {
                "/html/body/div/main/div/form/div[1]/table/tbody/tr/td[1]/div/div/div/label[1]",
                "展开",
                "/html/body/div/main/div/form/div[3]/div[2]/table/tbody/tr[1]/td/div/div/div/label[1]",
                "/html/body/div/main/div/form/div[4]/div[2]/table/tbody/tr[1]/td/div/div/div/label[1]"
        };
        Arrays.asList(radioList).forEach(radio->{
            if(radio.equals("展开")){
                try {
                    Thread.sleep(2000);
                    chrome.findElementByClassName("up").click();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else {
                WebDriverWait wait = new WebDriverWait(chrome, 5);
                wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(radio)));
                WebElement radioXpath = chrome.findElementByXPath(radio);
                radioXpath.click();
            }
        });
    }

    @Override
    public String getResult() {
        WebElement name = chrome.findElementByName("profile[xingming]");
        String userName = name.getAttribute("value");
        WebElement xuegonghao = chrome.findElementByName("profile[xuegonghao]");
        String xuehao = xuegonghao.getAttribute("value");
        WebElement button = chrome.findElementByXPath("/html/body/div/main/div/form/div[6]/button");
        button.click();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        WebElement result = chrome.findElementByClassName("card-header");
        String text = result.getText();
        chrome.close();
        System.out.println(userName+xuehao+text);
        return xuehao+userName+text;
    }

    @Override
    public void doHealthReport() {
        String[] userInfoList = userInfo.split(";");
        Arrays.asList(userInfoList).stream().map(userInfo->userInfo.split(":")).forEach(user->{
            login(user[0],user[1]);
            doReport();
            String result = getResult();
            emailUtil.sendEmail(user[2],new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"日东大健康报备已成功",result);
        });
    }
}
